/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.book.web.in;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.base.entity.book.BaseBook;
import com.thinkgem.jeesite.modules.base.entity.card.BaseCard;
import com.thinkgem.jeesite.modules.base.service.book.BaseBookService;
import com.thinkgem.jeesite.modules.base.service.card.BaseCardService;
import com.thinkgem.jeesite.modules.book.entity.out.BookOut;
import com.thinkgem.jeesite.modules.book.service.out.BookOutService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.book.entity.in.BookIn;
import com.thinkgem.jeesite.modules.book.service.in.BookInService;

/**
 * 借阅管理_退还生成Controller
 * @author 呆子
 * @version 2020-02-22
 */
@Controller
@RequestMapping(value = "${adminPath}/book/in/bookIn")
public class BookInController extends BaseController {

	@Autowired
	private BookInService bookInService;
    @Autowired
    private BaseCardService baseCardService;
    @Autowired
    private BaseBookService baseBookService;
    @Autowired
    private BookOutService bookOutService;
	
	@ModelAttribute
	public BookIn get(@RequestParam(required=false) String id) {
		BookIn entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bookInService.get(id);
		}
		if (entity == null){
			entity = new BookIn();
		}
		return entity;
	}
	
	@RequiresPermissions("book:in:bookIn:view")
	@RequestMapping(value = {"list", ""})
	public String list(BookIn bookIn, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BookIn> page = bookInService.inQueryPage(new Page<BookIn>(request, response), bookIn);
		model.addAttribute("page", page);
		return "modules/book/in/bookInList";
	}

	@RequiresPermissions("book:in:bookIn:view")
	@RequestMapping(value = "form")
	public String form(BookIn bookIn, Model model) {
		model.addAttribute("bookIn", bookIn);
		return "modules/book/in/bookInForm";
	}

    //根据Id查询并返回至form
    @RequestMapping(value = "inQueryManyForm")
    public String inQueryManyForm(BookIn bookIn, Model model) {
        BookIn bookInForm = bookInService.inQueryManyForm(bookIn);
        model.addAttribute("bookIn", bookInForm);
        return "modules/book/in/bookInForm";
    }

	@RequiresPermissions("book:in:bookIn:edit")
	@RequestMapping(value = "save")
    @Transactional
	public String save(BookIn bookIn, Model model, RedirectAttributes redirectAttributes) {
        bookIn.setInValid(0);
		if (!beanValidator(model, bookIn)){
			return form(bookIn, model);
		}

		try {
            //退还__保存时将baseCard中cardBorNum+1,cardBoredNum-1
            BaseCard baseCard = new BaseCard();
            baseCard.setCardId(bookIn.getInCardId());
            baseCard = baseCardService.findCardByCardId(baseCard);
            baseCard.setCardBorNum(baseCard.getCardBorNum()+1);
            baseCard.setCardBoredNum(baseCard.getCardBoredNum()-1);
            baseCardService.save(baseCard);

            //退还__保存时将baseBook的bookNum+1
            BaseBook baseBook = new BaseBook();
            baseBook.setId(bookIn.getInBookId());
            baseBook = baseBookService.findBookById(baseBook);
            baseBook.setBookNum(baseBook.getBookNum()+1);
            baseBookService.save(baseBook);


            System.out.println("================================>>>>>>>>>>>>>>"+bookIn.getBookOutId());
            //退还__保存时将bookOut的outValid设置为1(无效即已还书)
            BookOut bookOut = new BookOut();
            bookOut.setId(bookIn.getBookOutId());
            bookOutService.updateOutValidNo(bookOut);
            System.out.println("================================>>>>>>>>>>>>>>"+bookIn.getBookOutId());
            bookInService.save(bookIn);
            addMessage(redirectAttributes, "保存退还记录成功");
        }catch (Exception e){
		    e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            addMessage(redirectAttributes, "保存退还记录失败");
        }

		return "redirect:"+Global.getAdminPath()+"/book/in/bookIn/?repage";
	}
	
	@RequiresPermissions("book:in:bookIn:edit")
	@RequestMapping(value = "delete")
	public String delete(BookIn bookIn, RedirectAttributes redirectAttributes) {
		bookInService.delete(bookIn);
		addMessage(redirectAttributes, "删除借阅管理_退还成功");
		return "redirect:"+Global.getAdminPath()+"/book/in/bookIn/?repage";
	}

}